home *** CD-ROM | disk | FTP | other *** search
- Path: dawn.mmm.com!news
- From: kjhopps@mmm.com (Kevin J Hopps)
- Newsgroups: comp.lang.c++
- Subject: Re: Constructor Exceptions
- Date: 8 Jan 1996 15:25:17 GMT
- Organization: 3M - St. Paul, MN 55144-1000 US
- Message-ID: <4crd0t$9oh@dawn.mmm.com>
- References: <4bud9g$pv5@oxy.rust.net> <4cbhcl$kst@dawn.mmm.com> <4ce68n$8u4@sundog.tiac.net> <4ch1c3$bsm@oxy.rust.net>
- Reply-To: kjhopps@mmm.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Paul Gunn (pgunn@mail.cbf.com) wrote:
- > Here is an excerpt from the Visual C++ 2.1 documentation. It seems to
- > indicate that 'this' will not be cleaned up.
-
- > >
- > Throwing an exception in a constructor is tricky, however, because the
- > memory for the object itself has already been allocated by the time
- > the constructor is called. There is no simple way to deallocate the
- > memory occupied by the object from within the constructor for that
- > object. Thus, you will find that throwing an exception in a
- > constructor will result in the object remaining allocated. For a
- > discussion of how to detect objects in your program that have not been
- > deallocated, see the article Diagnostics: Detecting Memory Leaks.
- > If you are performing operations in your constructor that can fail, it
- > might be a better idea to put those operations into a separate
- > initialization function rather than throwing an exception in the
- > constructor. That way, you can safely construct the object and get a
- > valid pointer to it. Then, you can call the initialization function
- > for the object. If the initialization function fails, you can delete
- > the object directly.
- > <
-
- > What do you think?
-
- From the ANSI C++ Draft dated April 28, 1995 section 5.3.4 [expr.new],
- paragraph 17:
- If the constructor throws an exception and the new-expression does not
- contain a new-placement, then the deallocation function
- (_basic.stc.dynamic.deallocation_, _class.free_) is used to free the
- memory in which the object was being constructed, after which the
- exception continues to propagate in the context of the new-expression.
-
- The standard makes no provisions for problems which have "no simple way" of
- being solved :-)
- --
- Kevin J. Hopps e-mail: kjhopps@mmm.com
- 3M Company phone: (612) 737-4643
- 3M Center, Bldg. 235-2D-57 fax: (612) 737-2700
- St. Paul, MN 55144-1000 Opinions are my own. I don't speak for 3M.
- But 3M speaks for me -- I did not write the following line:
-
- Opinions expressed herein are my own and may not represent those of 3M.
-